package com.fyt.service.Impl.user.account;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fyt.controller.utils.Code;
import com.fyt.entity.User;
import com.fyt.mapper.UserMapper;
import com.fyt.service.Impl.utils.UserDetailsImpl;
import com.fyt.service.user.account.InfoService;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class InfoServiceImpl extends ServiceImpl<UserMapper,User> implements InfoService{
    @Override
    public JSONObject getInfo() {
        //从Security上下文获取存储的user信息
        UsernamePasswordAuthenticationToken authentication =
                (UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
        UserDetailsImpl loginUser = (UserDetailsImpl) authentication.getPrincipal();
        User user = loginUser.getUser();
        JSONObject resp = new JSONObject();
        resp.put("code",user==null? Code.get_err:Code.get_ok);
        resp.put("message",user==null?"success":"error");
        resp.put("data",user);
        return resp;
    }
}
